package tum0r.data.structure;

import tum0r.misc.Copy;

import java.util.LinkedList;

/**
 * 工程: JavaTools<br>
 * 包: tum0r.data.structure<br>
 * 创建者: tum0r<br>
 * 创建时间: 2020/8/21 23:23<br>
 * <br>
 */
public class Queue<T> extends Copy<Queue<T>> {
    private final LinkedList<T> list;

    public Queue() {
        list = new LinkedList<>();
    }

    public boolean push(T data) {
        if (list.size() < Integer.MAX_VALUE) {
            list.addLast(data);
            return true;
        }
        return false;
    }

    public T pop() {
        if (list.size() > 0) {
            T data = list.getFirst();
            list.removeFirst();
            return data;
        }
        return null;
    }

    public boolean isEmpty() {
        return list.size() == 0;
    }

    public int size() {
        return list.size();
    }

    public T getFront() {
        return list.getFirst();
    }

    public T getBack() {
        return list.getLast();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof Queue && this.size() == ((Queue<T>) obj).size()) {
            boolean equals = true;
            Queue<T> thisCopy = null;
            Queue<T> objCopy = null;
            try {
                thisCopy = this.deepCopy();
                objCopy = ((Queue<T>) obj).deepCopy();
            } catch (InstantiationException | IllegalAccessException | SecurityException | IllegalArgumentException e) {
                e.printStackTrace();
            }
            if (thisCopy != null && objCopy != null) {
                while (!thisCopy.isEmpty()) {
                    if (!thisCopy.pop().equals(objCopy.pop())) {
                        return false;
                    }
                }
            }
            return true;
        }
        return false;
    }
}
